clear

import delimited "Data_prepared_for_frm_in_STATA\frm_data_electoral_safety_v4.csv"

generate classified_tweets_share_main_iss_n = real(classified_tweets_share_main_iss)

generate electoral_safety_f = real(electoral_safety)

gen which_elec_safety_f = which_elec_safety if inlist(which_elec_safety, "dist", "list")
encode which_elec_safety_f, generate(which_elec_safety_ff)

encode partyname, generate(partyname_f)
tabulate partyname_f, generate(partyname_dummy)

gen politician_numb_of_class_t_f = politician_number_of_classified_

gen electoral_safety_2nd_f = real(electoral_safety_2nd)

// estimate fractional logit model using the user-written frm command
// frm tweets_share_main_issue electoral_safety_f which_elec_safety_ff, linkfrac(loglog)

frm classified_tweets_share_main_iss electoral_safety_f which_elec_safety_ff electoral_safety_2nd_f only_dist politician_numb_of_class_t_f sex age partyname_dummy2 partyname_dummy3 partyname_dummy4  partyname_dummy5  partyname_dummy6, linkfrac(loglog)
estimates store frac_loglog

// output regression table
outreg2 using tab_a10_electoral_security.doc, replace ctitle(Model 1)

// perform RESET test using two powers
frm_reset, l(2)

// perform GOF tests
frm_ggoff // Stat 0.340   pvalue 0.5601

// compute APE
margins, dydx(electoral_safety_f)
// generate plot
margins, at(electoral_safety_f = (0 (0.05) 1))   saving("Data_frm_results_from_STATA\electoral_safety_mod4_data_margins_with_afd.dta")
marginsplot
